package org.zachary.agent.application;

import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.zachary.agent.application.exception.NoSuchUserException;
import org.zachary.agent.application.exception.UserAlreadyExists;
import org.zachary.agent.domain.aggregate_model.user.User;
import org.zachary.agent.domain.repository.UserRepository;
import org.zachary.agent.infrastructure.repository.UserIdCountryProjection;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserService {
    private final UserRepository userRepo;

    public UserService(UserRepository userRepo) {
        this.userRepo = userRepo;
    }

    @Transactional
    public User getUserInfo(String uuid) throws Exception {
        User user=userRepo.findByUuid(uuid);
        if (user==null) {
            throw new NoSuchUserException("没有此用户");
        }
        return user;
    }
    @Transactional
    public void updateUserInfo(String uuid,User user) throws Exception {
        User u=userRepo.findByUuid(uuid);
        if(u==null) {
            throw new IllegalArgumentException("无效的UUID");
        }
        user.improveInfo();

        userRepo.improveUserInfo(uuid,user);
    }
    @Transactional
    public void updateUserAvatar(Integer userId, String url) {
        userRepo.updateAvatar(userId,url);
    }
    @Transactional
    public User findByUserId(Integer userId) {
        return userRepo.findById(userId);
    }

    public Map<Integer, UserIdCountryProjection> findCountyByIds(List<Integer> ids) {
        return userRepo.loadCountyByIds(ids);
    }
}
